#include "plr.h"
#include "rectype.h"
#include "util.h"

#include "nmfield.h"
#include "nmfield.cpp"

#include "nmarrfield.h"
#include "nmarrfield.cpp"

#include "cnarrfield.h"
#include "cnarrfield.cpp"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Plr::Plr()
{
}

Plr::Plr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf )
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf != 0)
        readFields(buf);
}

Plr::~Plr(){
}

const StdfField<int> Plr::getGroupCount() const {
    return grp_cnt;
}

void Plr::setGroupCount(StdfField<int> grpcnt) {
    grp_cnt = grpcnt;
}

const StdfField<vector<unsigned int> > Plr::getGroupIndex() const {
    return grp_indx;
}

void Plr::setGroupIndex(StdfField<vector<unsigned int> > grpindx) {
    grp_indx = grpindx;
}

const StdfField<vector<unsigned int> > Plr::getGroupMode() const {
    return grp_mode;
}

void Plr::setGroupMode(StdfField<vector<unsigned int> > grpmode) {
    grp_mode = grpmode;
}

const StdfField<vector<unsigned int> > Plr::getGroupDisplayRadix() const {
    return grp_radx;
}

void Plr::setGroupDisplayRadix(StdfField<vector<unsigned int> > grpradx) {
    grp_radx = grpradx;
}

const StdfField<vector<string> > Plr::getProgramStateEncodeChars() const {
    return pgm_char;
}

void Plr::setProgramStateEncodeChars(StdfField<vector<string> > pgmchar) {
    pgm_char = pgmchar;
}

const StdfField<vector<string> > Plr::getReturnStateStateEncodeChars() const {
    return rtn_char;
}

void Plr::setReturnStateEncodeChars(StdfField<vector<string> > rtnchar) {
    rtn_char = rtnchar;
}

const StdfField<vector<string> > Plr::getProgramStateStateEncodeCharsL() const {
    return pgm_chal;
}

void Plr::setProgamStateEncodeCharsL(StdfField<vector<string> > pgmchal) {
    pgm_chal = pgmchal;
}

const StdfField<vector<string> > Plr::getReturnStateStateEncodeCharsL() const {
    return rtn_chal;
}

void Plr::setReturnStateEncodeCharsL(StdfField<vector<string> > rtnchal) {
    rtn_chal = rtnchal;
}

void Plr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= PLR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read group counter, u2
    NmField<int, 2> grpcnt(buf+curpos, en);
    grp_cnt.set(grpcnt.getData(), curpos, grpcnt.getByteCount(), grpcnt.getTotalByteCount());
    curpos += grp_cnt.getTotalByteCount();

    // Read Group index, U2 per item
    if (curpos < reclen -1) {
        NmArrField<unsigned int, 2> grpindx(buf+curpos, en, grp_cnt.getData());
        grp_indx.set(grpindx.getData(), curpos, grpindx.getByteCount(), grpindx.getTotalByteCount());
        curpos += grp_indx.getTotalByteCount();
    }

    // Read Group mode, U2 per item
    if (curpos < reclen -1) {
        NmArrField<unsigned int, 2> grpmode(buf+curpos, en, grp_cnt.getData());
        grp_mode.set(grpmode.getData(), curpos, grpmode.getByteCount(), grpmode.getTotalByteCount());
        curpos += grp_mode.getTotalByteCount();
    }

    // Read Group radix, U1 per item
    if (curpos < reclen -1) {
        NmArrField<unsigned int, 1> grpradx(buf+curpos, en, grp_cnt.getData());
        grp_radx.set(grpradx.getData(), curpos, grpradx.getByteCount(), grpradx.getTotalByteCount());
        curpos += grp_radx.getTotalByteCount();
    }

    // Read Program Character, array of string
    if (curpos < reclen -1) {
        CnArrField<string> pgmchar(buf+curpos, en, grp_cnt.getData());
        pgm_char.set(pgmchar.getData(), curpos, pgmchar.getByteCount(), pgmchar.getTotalByteCount());
        curpos += pgm_char.getTotalByteCount();
    }


    // Read Return Stage encoded Character, array of string
    if (curpos < reclen -1) {
        CnArrField<string> rtnchar(buf+curpos, en, grp_cnt.getData());
        rtn_char.set(rtnchar.getData(), curpos, rtnchar.getByteCount(), rtnchar.getTotalByteCount());
        curpos += rtn_char.getTotalByteCount();
    }

    // Read Return Program encoded Character 2, array of string
    if (curpos < reclen -1) {
        CnArrField<string> pgmchal(buf+curpos, en, grp_cnt.getData());
        pgm_chal.set(pgmchal.getData(), curpos, pgmchal.getByteCount(), pgmchal.getTotalByteCount());
        curpos += pgm_chal.getTotalByteCount();
    }


    // Read Return Stage encoded Character 2 , array of string
    if (curpos < reclen -1) {
        CnArrField<string> rtnchal(buf+curpos, en, grp_cnt.getData());
        rtn_chal.set(rtnchal.getData(), curpos, rtnchal.getByteCount(), rtnchal.getTotalByteCount());
    }
}
